package com.leapp.sk.ui.activity.personal

import android.content.Intent
import android.os.Bundle
import android.support.v4.widget.SwipeRefreshLayout
import android.view.View
import cc.vv.lkbasecomponent.http.LKHttp
import cc.vv.lkbasecomponent.http.lib.setting.Settings
import cc.vv.lklibrary.http.base.okhttpfactory.plugins.httpplugin.MediaTypeWrap
import com.example.baselibrary.util.LKToastUtil
import com.example.inter.RefreshLoadMoreInferface
import com.example.mvp.activity.BaseActivityMVP
import com.example.mvp.binder.BaseDataBinder
import com.leapp.sk.R
import com.leapp.sk.app.HELP_FEED_LIST
import com.leapp.sk.bean.FeedBackEntity
import com.leapp.sk.bean.request.personal.FeedBackListRequestEntity
import com.leapp.sk.bean.response.personal.FeedBackListResponseEntity
import com.leapp.sk.binder.BaseBinder
import com.leapp.sk.sever.personal.FeedBackSever
import com.example.util.finallist.SKCODE_200
import com.leapp.sk.viewdelegate.personal.FeedBackDelegate

/**
 * 反馈
 * @author  chaohaipeng  on 2018/8/9.
 */
class FeedbackActivity:BaseActivityMVP<FeedBackDelegate>(), RefreshLoadMoreInferface, SwipeRefreshLayout.OnRefreshListener {
    override fun onRefresh() {
        currentPage=1
        httpFeed(false)
    }

    /**
     * 当前页
     */
    var currentPage=1
    override fun loadMoreData() {
        currentPage++
        httpFeed(false)
    }

    val mutableListOf = mutableListOf<FeedBackEntity>()
    override fun bindEvenListener() {
        viewDelegate.setOnClickListener(View.OnClickListener {
            val intent= Intent(this,FeedProblemActiviaty::class.java)
            startActivity(intent)
        },R.id.rl_afb_btn)
    }
    override fun initView(p0: Bundle?) {
        viewDelegate.setTitle(resources.getString(R.string.string_feed_back_help))
    }

    override fun initDate() {
       viewDelegate.initAdapter(this,mutableListOf,this,this)
        httpFeed(true)

    }

    override fun getDelegateClass(): Class<FeedBackDelegate> {
        return FeedBackDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return BaseBinder()
    }

    /**
     * 获取反馈列表
     */
    private fun httpFeed(isShowLoder:Boolean) {
        val concatTeacherRequestEntity = FeedBackListRequestEntity()
        concatTeacherRequestEntity.currPage=currentPage
        LKHttp.post(HELP_FEED_LIST, concatTeacherRequestEntity, FeedBackListResponseEntity::class.java, object : BaseCallBack<FeedBackListResponseEntity>(this) {
            override fun onSuccess(url: String?, resultText: String?, obj: FeedBackListResponseEntity?) {
                super.onSuccess(url, resultText, obj)
                viewDelegate?.stopSwipRefresh()
                if (obj?.code == SKCODE_200) {
                    if (currentPage==1){
                        mutableListOf.clear()
                    }
                    val feedData = FeedBackSever.getInstance().getFeedData(obj,viewDelegate)
                    if (feedData!=null){
                        mutableListOf.addAll(feedData)
                    }
                    viewDelegate?.notifyAdapter()
                }else{
                    LKToastUtil.showToastShort(obj?.message)
                }
            }
            override fun onFailure(url: String?, isLoading: Boolean, exceptionStr: String?) {
                super.onFailure(url, isLoading, exceptionStr)
                viewDelegate?.stopSwipRefresh()
                LKToastUtil.showToastShort(resources.getString(R.string.app_net_erro))
            }
        }
                , isShowLoder, Settings(MediaTypeWrap.MEDIA_TYPE_FORM))

    }
}